import Queue from '../../utils/Queue'

const message = {
  state: {
    orgMsgs: new Queue(), // 存放最新的消息
    userMsgs: new Queue(), // 同上
    caseMsgs: {},
    newCase: null
  },
  mutations: {
    SET_NEW_MSG: (state, msg) => {
      if(msg.pushTarget === 'user')
        state.userMsgs.enqueue(msg);
      else if(msg.pushTarget === 'org')
        state.orgMsgs.enqueue(msg);

      // 新警情触发提醒，用在警情列表页面提醒并刷新列表
      if(msg.msgType==='case'
        && state.newCase != null){
        state.newCase.enqueue(msg);
      }
      // 为了防止不停推送数据，但是聊天窗口未打开来消费推送消息导致数据冗余积累，
      // 所以需要判断是否存在该警情编号，不存在就说明未打开聊天窗口，消息不存入
      if(msg.alarmUser && state.caseMsgs[msg.jjdbh]){
        state.caseMsgs[msg.jjdbh].enqueue(msg);
      }

    },
    DEL_CASE_MSG: (state, jjbh) => {
      delete state.caseMsgs[jjbh];
    },
    NEW_CASE_MSG_QUEUE: (state, jjbh) => {
      state.caseMsgs[jjbh] = new Queue();
    },
    DEL_CASE_QUEUE: (state) => {
      state.newCase = null;
    },
    NEW_CASE_QUEUE: (state) => {
      state.newCase = new Queue();
    }
  },
  actions: {
    // 缓存新消息
    pushNewMsg({commit, state}, msg) {
      commit('SET_NEW_MSG', msg);
    },
    // 删除警情消息缓存对象
    delCaseMsgObj({commit, state}, jjbh) {
      commit('DEL_CASE_MSG', jjbh);
    },
    //
    addNewCaseMsgQueue({commit, state}, jjbh) {
      commit('NEW_CASE_MSG_QUEUE', jjbh);
    },
    //
    delCaseQueue({commit, state}) {
      commit('DEL_CASE_QUEUE');
    },
    addNewCaseQueue({commit, state}) {
      commit('NEW_CASE_QUEUE');
    }
  }
};
export default message
